package com.founder.bdyx.core.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

public class SourcePath {
    static Logger logger = Logger.getLogger(SourcePath.class);
    /** class Constant **/
    final static String HTTP = "https://wangbin.xyz";
    final static String ST = "?md5=";
    final static String E = "&expires=";
    final static String F = "/";
    final static String WARN = "path参数最好带上 \"/\" ,例: \"/abc.mp4\" ";

    final static String SECRET = "wangbin";

    public static String execute(String path) {
        return execute(HTTP,path);
    }

    public static String execute(String http, String path){
        if (path.indexOf(F) == -1) {
            path = StringUtils.append(F,path);
            logger.warn(WARN);
        }
        String time = String.valueOf(System.currentTimeMillis() / 1000 + 300);// +n代表n秒后地址失效
        String md5 = Base64
                .encodeBase64URLSafeString(DigestUtils.md5(StringUtils.append(SECRET, time, path)));
        return StringUtils.append(http, path, ST, md5, E, time);
    }

    public static void main(String[] args) {
        System.out.println(execute("/aaa.pdf"));
    }
}
